package com.itany.controllers;

import com.itany.entity.ServerCompany;
import com.itany.entity.User;
import com.itany.service.ServerService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/server")
public class ServerController {
    @Autowired
    ServerService service;

    @RequestMapping("/findAll")
    public Map<String, Object> findAll(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "rows", defaultValue = "10") int rows,
            @RequestParam("name") String name,
            @RequestParam("flag") Integer flag,
            @RequestParam(value = "companyid", required = false) String companyid) {
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = service.queryByParams(name, flag, companyid);
        int total = list != null ? list.size() : 0;

        // 如果总数据量小于或等于每页显示的数据条数，则始终从第一页开始，且只显示一页
        if (total <= rows) {
            page = 1;
        } else {
            // 计算总页数，并检查请求的页码是否超出范围
            int totalPages = (int) Math.ceil((double) total / rows);
            if (page > totalPages) {
                page = 1; // 如果请求的页码大于总页数，则重置为第一页
            }
        }

        // 计算起始和结束索引
        int startIndex = (page - 1) * rows;
        int endIndex = Math.min(startIndex + rows, total);

        // 创建结果列表，并填充数据
        if(list!=null){
            List<Map<String, Object>> ref = list.subList(startIndex, endIndex);
            map.put("rows", ref);
        }else{
            map.put("rows",0);
        }

        map.put("total", total);

        return map;
    }

    @RequestMapping("/modifyStatus")
    public ActionResult modifyStatus(
            @RequestParam("id") Integer id,
            @RequestParam("managerUserId") Integer managerUserId){
        service.modifyStatus(id,managerUserId);
        return ActionResult.success("修改状态成功",200,null);
    }

    @RequestMapping("/findById")
    public ActionResult findById(@RequestParam("id") Integer id){
        Map<String,Object> map = service.findById(id);
        return ActionResult.success("查找成功",200,map);
    }

    @RequestMapping("/findInfo")
    public ActionResult findInfo(@RequestParam("id") Integer id){
        Map<String,Object> map = service.findInfo(id);
        return ActionResult.success("查找成功",200,map);
    }

    @RequestMapping("/modifyInfo")
    public ActionResult modifyInfo(
            @RequestParam("id") Integer id,
            @RequestParam("name") String name,
            @RequestParam("types") Integer[] types,
            @RequestParam("areas") Integer[] areas,
            @RequestParam("info") String info,
            @RequestParam(value = "imgs", required = false) MultipartFile[] imgs,
            @RequestParam(value = "existingImages", required = false) String existingImages){
        service.modifyInfo(id,name,types,areas,info,imgs,existingImages);
        return ActionResult.success("修改成功",200,null);
    }
}
